<template>
  <el-dialog
    width="500px"
    title="员工导入"
    :visible="showdialog"
    @close="$emit('update:showdialog', false)"
  >
    <el-row type="flex" justify="center" class="row">
      <input ref="file-input" type="file" class="fileInp" @change="uploadChange">
      <div class="center">
        <i class="el-icon-upload alicenter" />

        <el-button type="text" class="alicenter" @click="download">下载导入模板</el-button>
        <span class="alicenter btn">
          将文件拖到此处或
          <span class="upload">
            <el-button type="text" @click="upload">点击上传</el-button>
          </span>
        </span>

      </div>
    </el-row>
    <el-row type="flex" justify="end">
      <el-button type="primary" size="mini" @click="$emit('update:showdialog',false)">取消</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import FileSaver from 'file-saver'
import { getTemplates, importUser } from '@/api/employee'
export default {
  props: {
    showdialog: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    async download() {
      const data = await getTemplates()
      FileSaver.saveAs(data, '员工导入模板')
    },
    upload() {
      this.$refs['file-input'].click()
    },
    async uploadChange() {
      console.log(event.target.files)
      const files = event.target.files
      if (files.length > 0) {
        const data = new FormData()
        data.append('file', files[0])
        try {
          await importUser(data)
          this.$emit('change')
          this.$emit('update:showdialog', false)
        } catch (error) {
          console.log(error)
        } finally {
          this.$refs['file-input'].value = ''
        }
      }
    }
  }

}
</script>

<style scoped lang="scss">
.fileInp{
    display: none;
    z-index: -9999;
}
.row{
    width: 460px;
    height: 220px;
}
.center{
    width: 360px;
    height: 180px;
    border: 1px dashed #dcdfe6;
    display: flex;
    flex-direction: column;
    justify-content: center;
    margin: 20px auto;
}
.alicenter{
    text-align: center;
}
 .el-icon-upload{
    font-size: 60px;
    color: #c0c4cc;
}
.btn{
    font-size: 14px;
    color: #bbb;
}
.upload{
    padding-left: 1px;
}
</style>
